/**
 * Created by Celebration-Grey on 2017/5/8.
 */


admin.reply.$table = null;

admin.reply.$thead = null;

admin.reply.$tbody = null;

admin.reply.limit = 20;

admin.reply.maxIndex = null;

admin.reply.$author_info = null;

admin.reply.authorArray = [];

admin.reply.$reply_info = null;

admin.reply.parame = {
    orderBy : 'id',
    order : 'asc',
    index : 1,
    limit : admin.reply.limit
};

admin.reply.init = function(){
    admin.reply.$table = $('#reply-table');
    admin.reply.$thead = admin.reply.$table.find('thead');
    admin.reply.$reply_info = $('#reply-info');
    admin.reply.$author_info = $('#author-info');
    admin.reply.getReplys(admin.reply.getSuccess);
};

admin.reply.getReplys = function(fnSuccess){
    admin.ajax(admin.reply.urls.getReplys,admin.reply.parame,fnSuccess,admin.error);
};

admin.reply.getSuccess = function(data){
    if(data.status == 200){
        //admin.reply.parame.index++;
        admin.reply.total = data.data['total'];
        //console.log(admin.bowen.total);
        admin.reply.maxIndex = Math.ceil(admin.reply.total / admin.reply.limit);

        admin.reply.pageChange();
        admin.reply.fillHead(data.data['replys'][0]);
        admin.reply.fillDatas(data.data['replys']);
        admin.reply.page();
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.reply.fillHead = function(data){
    var field = admin.reply.field;
    var $tr = $('<tr></tr>');
    for (var key in data){
        var $td = $('<th class="'+key+'">'+field[key]+'</th>');
        $tr.append($td);
    }
    $tr.append('<th class="action">操作</th>');
    admin.reply.$thead.append($tr);
};

admin.reply.fillDatas = function(data){
    admin.reply.$table.find('tbody').remove();
    var $tbody = $('<tbody></tbody>');
    for(var i = 0; i < data.length; i++){
        $tbody.append(admin.reply.fillData(data[i]));
    }
    admin.reply.$table.append($tbody);
};

admin.reply.fillData = function(data){
    var $tr = $('<tr></tr>');
    for(var key in data){
        var $td = $('<td class="'+key+'" title="'+data[key]+'">'+data[key]+'</td>');
        if(key == 'reply_content'){
            $td = $('<td class="'+key+'" title="'+htmlEncodeByRegExp(data[key])+'">'+htmlEncodeByRegExp(data[key])+'</td>');
        }
        $tr.append($td);
    }
    var $action = $('<td class="action"><span class="icon Hui-iconfont show">&#xe665;</span><span class="icon Hui-iconfont edit">&#xe60c;</span><span class="icon Hui-iconfont delete">&#xe6e2;</span></td>');
    $tr.append($action);

    $tr.find('.show').click(function () {
        admin.reply.showReplyInfo(data);
        admin.reply.showAuthorInfo(data['reply_user_id']);
        modalshow();
    });

    return $tr;
};


admin.reply.showReplyInfo = function (data) {
    admin.reply.$reply_info.find('tbody').remove();
    var field = admin.reply.field;
    var $tbody = $('<tbody></tbody>');
    for(var key in data){
        var $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+data[key]+'">'+data[key]+'</td></tr>');
        $tbody.append($tr);
    }
    admin.reply.$reply_info.append($tbody);
};

admin.reply.showAuthorInfo = function (aid) {
    for(var i = 0; i < admin.reply.authorArray.length; i++){
        if(aid == admin.reply.authorArray[i]['user_id']){
            admin.reply.fillAuthorInfo(admin.reply.authorArray[i]);
            return true;
        }
    }
    admin.reply.$author_info.find('tbody').remove();
    admin.reply.getAuthorInfo(aid);
};

admin.reply.fillAuthorInfo = function (data) {
    admin.reply.$author_info.find('tbody').remove();
    admin.reply.$author_info.append(admin.reply.fillAuthorTbody(data));
};

admin.reply.getAuthorInfo = function (aid) {
    $.ajax({
        url : admin.bowen.urls.getAuthor,
        type:'POST',
        dataType:'json',
        data:{
            author_id : aid
        },
        xhrFields: {withCredentials: true},
        success:admin.reply.getAuthorSuccess,
        error:admin.error
    })
};

admin.reply.getAuthorSuccess = function (data) {
    if(data.status == 200){
        //admin.bowen.fillAuthorTbody(data.data);
        admin.reply.authorArray.push(data.data);
        //console.log(admin.bowen.authorArray);
        admin.reply.fillAuthorInfo(data.data);
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.reply.fillAuthorTbody = function (data) {
    var field = admin.user.field;
    var $tbody = $('<tbody></tbody>');
    for(var key in data){
        var $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+data[key]+'">'+data[key]+'</td></tr>');
        if(key == 'user_head_img'){
            var src = data[key];
            src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="avatar size-L radius" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
        }
        else if( key == 'user_card_img' || key == 'user_bg_img'){
            var src = data[key];
            src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
        }
        else if( key == 'user_city' ){
            var info = data[key];
            for(var i = 0; i < City.length; i++){
                if(info == City[i]['CityID']){
                    info = City[i]['name'];
                    break;
                }
            }
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
        }
        else if( key == 'user_province'){
            var info = data[key];
            for(var i = 0; i < Province.length; i++){
                if(info == Province[i]['ProID']){
                    info = Province[i]['name'];
                    break;
                }
            }
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
        }
        $tbody.append($tr);
    }
    return $tbody;
};

admin.reply.pageChange = function () {
    var $index = $('span.index');
    var $total = $('span.total');
    $index.html(admin.reply.parame.index);
    $total.html(admin.reply.maxIndex);

    if(admin.reply.parame.index <= 1){
        $('button.prev').addClass('disabled');
    }

    if(admin.reply.parame.index >= admin.reply.maxIndex){
        $('button.next').addClass('disabled');
    }
};

admin.reply.page = function () {
    var $prev = $('button.prev');
    var $next = $('button.next');

    $next.click(function () {
        $(this).addClass('disabled');
        if(admin.reply.parame.index < admin.reply.maxIndex){
            admin.reply.parame.index++;
        }
        admin.reply.getReplys(admin.reply.nextSuccess);
    });

    $prev.click(function () {
        $(this).addClass('disabled');
        if(admin.reply.parame.index > 1){
            admin.reply.parame.index--;
        }
        admin.reply.getReplys(admin.reply.prevSuccess);
    });
};

admin.reply.nextSuccess = function (data) {
    if(data.status == 200){
        admin.reply.pageChange();
        admin.reply.fillDatas(data.data['replys']);
        if(admin.reply.parame.index >= admin.reply.maxIndex){
            $('button.next').addClass('disabled');
        }
        else{
            $('button.next').removeClass('disabled');
        }
        $('button.prev').removeClass('disabled');
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.reply.prevSuccess = function (data) {
    if(data.status == 200){
        admin.reply.pageChange();
        admin.reply.fillDatas(data.data['replys']);
        if(admin.reply.parame.index <= 1){
            $('button.prev').addClass('disabled');
        }
        else{
            $('button.prev').removeClass('disabled');
        }
        $('button.next').removeClass('disabled');
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};